// ProviderList.cpp : implementation file
//

#include "stdafx.h"
#include "CustomerListDlg.h"
#include "MySQL/CustomerTbl.h"



// CProviderListDlg dialog

IMPLEMENT_DYNAMIC(CCustomerListDlg, CDialog)

CCustomerListDlg::CCustomerListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCustomerListDlg::IDD, pParent)
{
	m_pSelected = NULL;
}

CCustomerListDlg::~CCustomerListDlg()
{
}

void CCustomerListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_CUSTOMER, m_ctrlList);
}


BEGIN_MESSAGE_MAP(CCustomerListDlg, CDialog)
	ON_BN_CLICKED(IDC_BUT_ADD_PROV, &CCustomerListDlg::OnBnClickedButAddProv)
	ON_LBN_DBLCLK(IDC_LIST_CUSTOMER, &CCustomerListDlg::OnLbnDblclkListProvider)
	ON_BN_CLICKED(IDC_BUT_EXIT, &CCustomerListDlg::OnBnClickedButExit)
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_BUTT_REFRESH, &CCustomerListDlg::OnBnClickedButtRefresh)
END_MESSAGE_MAP()


// CProviderListDlg message handlers

void CCustomerListDlg::OnBnClickedButAddProv()
{
	// TODO: Add your control notification handler code here
}

BOOL CCustomerListDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	CPoint pt;
	GetCursorPos(&pt);
	SetWindowPos(NULL, pt.x, pt.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
	loadCustomer();
	return TRUE;  // return TRUE unless you set the focus to a control
}

void CCustomerListDlg::loadCustomer()
{
	vector<CCustomer*> CustomerList;
	CustomerList = CCustomerTable::getListCustomerByName(_T(""));
	int nSize = CustomerList.size();
	UpdateData(TRUE);
	m_ctrlList.ResetContent();
	for (int i =0; i <nSize; i++)
	{
		CCustomer* pCustomer  = CustomerList.at(i);
		if (pCustomer)
		{
			int nIndex = m_ctrlList.AddString(pCustomer->getName());
			m_ctrlList.SetItemData(nIndex,(DWORD_PTR)pCustomer);
		}
	}
	UpdateData(FALSE);
}


void CCustomerListDlg::OnLbnDblclkListProvider()
{
	// TODO: Add your control notification handler code here
	int nSel = m_ctrlList.GetCurSel();
	if (nSel != -1)
	{
		m_pSelected = (CCustomer*)m_ctrlList.GetItemData(nSel);
		CDialog::OnOK();
	}
}

void CCustomerListDlg::OnBnClickedButExit()
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}



BOOL CCustomerListDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
	{
		pMsg->wParam = NULL;
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CCustomerListDlg::OnBnClickedButtRefresh()
{
	// TODO: Add your control notification handler code here
}
